import React from 'react';
import {Table, Card} from 'antd';
import PropTypes from 'prop-types';
import routes from './routes';
import {Link} from 'react-router-dom';
import Ellipsis from 'ant-design-pro/lib/Ellipsis';
import {conventEnumValueToString, formatAmount, formatPercent} from '../utils';
import {currency} from '../Enum';
import {TABLE, PAGINATION_OPTIONS, COMMON_TABLE_QUERY_FAIL_TEXT, COMMON_TABLE_EMPTY_TEXT, DATE_FORMAT} from '../constants';
const basePath = fetch.basePath || '';
const url = '/#/dcs/parts/partStockReport';
export class TablePanel extends React.PureComponent {
    constructor(props) {
        super(props);
        this.onChangeSort = this.onChangeSort.bind(this);
        this.state = {
            loading: false
        };
    }
    onChangeSort = (pagination, filters, sorter) => {
        this.props.onChangeSort({
            pageIndex: pagination.current - 1,
            pageSize: pagination.pageSize,
            sortedColumnKey: sorter.columnKey,
            sortedOrder: sorter.order,
        });
    };
    onClickDetail = (month, dealerId) => {
        this.props.getDetail(month, dealerId);
    }
    render() {
        const {formatMessage} = this.props.intl;
        const {pageIndex, pageSize, total} = this.props;
        const locale = {
            emptyText: this.props.hasError ? COMMON_TABLE_QUERY_FAIL_TEXT : COMMON_TABLE_EMPTY_TEXT
        };
        const pagination = {
            total,
            pageSize,
            current: pageIndex + 1,
            ...PAGINATION_OPTIONS
        };
        const columns = [{
            title: formatMessage({
                id: 'tablePanel.column.marketDepartmentName',
                defaultMessage: '大区'
            }),
            dataIndex: 'marketDepartmentName',
            sorter: true,
            sortOrder: this.props.sortedColumnKey === 'marketDepartmentName' && this.props.sortedOrder,
        }, {
            title: formatMessage({
                id: 'tablePanel.column.currency',
                defaultMessage: '币种'
            }),
            dataIndex: 'currency',
            sorter: true,
            sortOrder: this.props.sortedColumnKey === 'currency' && this.props.sortedOrder,
            render: text => conventEnumValueToString(currency, text)
        }, {
            title: formatMessage({
                id: 'tablePanel.column.country',
                defaultMessage: '国家'
            }),
            dataIndex: 'country',
        }, {
            title: formatMessage({
                id: 'tablePanel.column.month',
                defaultMessage: '月份'
            }),
            dataIndex: 'month',
            sorter: true,
            sortOrder: this.props.sortedColumnKey === 'month' && this.props.sortedOrder,
            render: (text, record) => <a key="detail" href={`${basePath}${url}/${record.id}/detail?month=${record.month}&year=${record.year}&dealerId=${record.dealerId}`}>{`${record.year}-${record.month}`}</a>
        }, {
            title: formatMessage({
                id: 'tablePanel.column.stockKind',
                defaultMessage: '库存品种'
            }),
            dataIndex: 'stockKind',
        }, {
            title: formatMessage({
                id: 'tablePanel.column.stockPrice',
                defaultMessage: '库存金额（万元）'
            }),
            dataIndex: 'stockPrice',
            render: text => formatAmount(text)
        }, {
            title: formatMessage({
                id: 'tablePanel.column.saleKind',
                defaultMessage: '销售品种'
            }),
            dataIndex: 'saleKind',
        }, {
            title: formatMessage({
                id: 'tablePanel.column.salePrice',
                defaultMessage: '销售金额（万元）'
            }),
            dataIndex: 'salePrice',
            render: text => formatAmount(text)
        }, {
            title: formatMessage({
                id: 'tablePanel.column.stockToUseRatio',
                defaultMessage: '存销比'
            }),
            dataIndex: 'stockToUseRatio',
        }];
        if(!this.props.isDealerUser)
            columns.splice(0, 0, ...[
                {
                    title: this.props.intl.formatMessage({
                        id: 'tablePanel.column.dealerCode',
                        defaultMessage: '总代ERP号'
                    }),
                    dataIndex: 'dealerCode',
                    sorter: true,
                    sortOrder: this.props.sortedColumnKey === 'dealerCode' && this.props.sortedOrder,
                }
            ]);
        return (
            <Card>
                <Table
                    className={'white-space-nowrap'}
                    columns={columns}
                    dataSource={this.props.data}
                    loading={this.props.loading || this.state.loading}
                    onChange={this.onChangeSort}
                    pagination={pagination}
                    rowKey="id"
                    locale={locale}
                    {...TABLE}/>
            </Card>
        );
    }
}
TablePanel.propTypes = {
    data: PropTypes.array.isRequired,
    hasError: PropTypes.string.isRequired,
    loading: PropTypes.bool.isRequired,
    total: PropTypes.number.isRequired,
    onChangeSort: PropTypes.func.isRequired,
    intl: PropTypes.object,
    isDealerUser: PropTypes.bool,
    pageIndex: PropTypes.number,
    pageSize: PropTypes.number,
    sortedColumnKey: PropTypes.string,
    sortedOrder: PropTypes.string,
};
import {connect} from 'react-redux';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
import {tableSearch} from './actions';
import {injectIntl} from './intl';

const getData = selectorFactory(['page', 'domainData', 'list', 'data']);
const mapStateToProps = state => ({
    data: getData(state),
    loading: state.getIn(['page', 'domainData', 'list', 'isFetching']),
    hasError: state.getIn(['page', 'domainData', 'list', 'hasError']),
    total: state.getIn(['page', 'domainData', 'list', 'total']),
    pageIndex: state.getIn(['page', 'appState', 'queryBySort', 'pageIndex']),
    pageSize: state.getIn(['page', 'appState', 'queryBySort', 'pageSize']),
    sortedColumnKey: state.getIn(['page', 'appState', 'queryBySort', 'sortedColumnKey']),
    sortedOrder: state.getIn(['page', 'appState', 'queryBySort', 'sortedOrder']),
    isDealerUser: state.getIn(['page', 'domainData', 'init', 'isDealerUser']),
});

const mapDispatchToProps = (dispatch, props) => ({
    onChangeSort: option => dispatch(tableSearch(option)),

});

export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(TablePanel));
